$(document).ready(function(){
    var id = $('#reader').attr("value");
    var text = $('#reader').text();
    if(text == have ) {
        $('#reader').live('click',function () {
            addBook(id);
        });
    }
    if(text == havent ) {
        $('#reader').live('click',function () {
            removeBook(id);
        });
    }
});

var have = "I have read this";
var havent = "I haven't read this";

function addBook( id ){                //is called when user chooses I have read, adds the book to the users list of read books                                         
    $.post("DBServlet", {
        action: "addReadBook" , 
        book: id.toString()
        }, function(data) {
        if(data == "true" ){
            var r = reader;
            $('#reader').text( havent );
            $('#reader').live('click',function(){
                removeBook(id);
            });
        }
    })
}
  
function removeBook( id ){            //is called when user chooses I have't read or when removing from read books                         
    $.post("DBServlet", {
        action: "removeReadBook" , 
        book: id.toString()
        }, function(data) {
        if(data == "true" ){
            var r = $("reader");
            $('#reader').text( have );
            $('#reader').live('click',function (){
                addBook(id);
            });
        }
    })
}